package megrez.job.custom.tasks;

import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
import megrez.job.custom.domain.LogRepository;

/**
 * 清除日志任务
 *  
 * @author Lucky Yang
 * @since 2.6.9
 */
@Slf4j
@Component
public class ClearLogTask {
   @Autowired
   private LogRepository logRep;
   
   
   public void execute(String keepOfMinutes) {
     int limit = Integer.parseInt(keepOfMinutes);

     // 最少10分钟
     if (limit < 10) {
        limit = 10;
     }

     Calendar now = Calendar.getInstance();
     now.add(Calendar.MINUTE, limit * -1);

     Date deleteData = now.getTime();
     if (log.isDebugEnabled()) {
        log.debug("Remove logs before {}", deleteData);
     }

     int deleteRowCount = logRep.deleteByCreatedDateLessThan(deleteData);
     if (log.isDebugEnabled()) {
        log.debug("{} rows were removed", deleteRowCount);
     }
   }
}
